''' 图片检测，分开.jpg和.txt，并将漏检和误检放在各自的文件夹'''

import os
from tqdm import tqdm
import shutil
import os.path

mix_file_path = r'/home/xys/larger_objet_Projects/dataset/20211120/20211120_data_extract/data_detector/'  #jpg和txt混合路径
all_txt_path = r'/home/xys/larger_objet_Projects/dataset/20211120/20211120_data_extract/txt预测文件夹/'     #txt保存路径
no_detector_path = r'/home/xys/larger_objet_Projects/dataset/20211120/20211120_data_extract/漏检测/'       #图片路径
leak_detector_path = r'/home/xys/larger_objet_Projects/dataset/20211120/20211120_data_extract/误检测/'     #误检图片保存路径
threshold = 0.3

file_names = os.listdir(mix_file_path)
# lens =  len(file_names)

for file in tqdm(file_names):                                                  # 分开txt和img
    file_back = file[-4:]
    if file_back not in ['.jpg']:
        #shutil.copy(os.path.join(mix_file_path, file), no_detector_img_path)  # 将文件复制到设定的文件夹中
        shutil.move(os.path.join(mix_file_path, file), all_txt_path)     #将文件移动到设定的文件夹中


file_txt_names = os.listdir(all_txt_path)
for file in tqdm(file_txt_names):
    # file_front = file[:-4]
    dir = os.path.join(all_txt_path,file)
    if os.path.getsize(dir) == 0:                                        #区分漏检
        shutil.copy(os.path.join(mix_file_path, file+'.jpg'), no_detector_path)  # 将文件复制到设定的文件夹中
        #shutil.move(os.path.join(mix_file_path, file_front+'.jpg'), no_detector_path)  # 将文件移动到设定的文件夹中
    else:                                                                #区分误检
        with open(dir, 'r') as f:
            l = f.readlines()
            for item in l:
                prob = float(item[-7:])
                if prob <= threshold:
                    shutil.copy(os.path.join(mix_file_path, file + '.jpg'), leak_detector_path)  # 将图像复制到设定的文件夹中
                    shutil.copy(os.path.join(all_txt_path, file), leak_detector_path)  # 将文本复制到设定的文件夹中
                    # shutil.move(os.path.join(mix_file_path, file_front+'.jpg'), no_detector_path)  # 将文件移动到设定的文件夹中
                    break